package tgc.tdu.tgq.demo.custom;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

public class TablePageable {
	private Integer page;
	private Integer rows;
	private String sort;
	private String order;
	
	public Integer getPage() {
		return page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public Integer getRows() {
		return rows;
	}
	public void setRows(Integer size) {
		this.rows = size;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	
	private void init() {
		page=page==null?0:page-1;
		rows=rows==null?10:rows;
		sort=sort==null?"id":sort;
		order=order==null?"asc":order;
	}
	
	public Sort buildSort() {
		init();
		return Sort.by(Direction.fromString(order), sort);
	}
	
	public PageRequest buildPageRequest() {
		Sort sort2=buildSort();
		return PageRequest.of(page, rows, sort2);
	}
	
	public PageRequest buildPageRequest(Sort sort) {
		init();
		return PageRequest.of(page, rows,sort);
	}
}
